package 综合应用.T8;

import cn.itcast.xml.XmlUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class T8 {

    public static void main(String[] args) throws IOException {

        List<User> userList = XmlUtils.readAll("综合应用/T8/user.xml", User.class);
        List<Product> product = getProduct();
        List<Category> category = getCategory(product);

        System.out.println("===================================网上商城管理系统===================================");
        while (true) {
            System.out.println("======================================================================");
            System.out.println("【1】登录");
            System.out.println("【2】退出");

            Scanner sc = new Scanner(System.in);
            int num = sc.nextInt();

            switch (num) {
                case 1:
                    Register(userList,category,product);
                    break;
                case 2:
                    System.exit(0);
                    break;
                default:
                    System.out.println("请输入正确数字");
            }
        }

    }



    // 1.4---(后台管理平台)---【4】删除指定商品类别
    public static void Dtspc( List<Category> category){
        System.out.println("===================================欢迎使用此功能===================================");
        System.out.println("类别id\t\t类别名称");
        for (Category c : category) {
            System.out.println(c.getId()+"\t\t"+c.getCname());
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的类别编号：");
        String num = sc.nextLine();
        category.removeIf(c -> c.getId().equals(num));


        ArrayList<Category>categories = new ArrayList<>();
        for (Category c : category) {
            categories.add(new Category(c.getId(),c.getCname()));
        }
        ArrayList<Product>products = new ArrayList<>();
        for (Category c : category) {
            List<Product> productList = c.getProductList();
            for (Product p : productList) {
                products.add(new Product(p.getCid(),p.getId(),p.getPname(),p.getState()));
            }
        }
        XmlUtils.write("综合应用/T8/category.xml",categories,false);
        XmlUtils.write("综合应用/T8/product.xml",products,false);

    }

    // 1.4---(后台管理平台)---【3】指定商品下架
    public static void Dmrfs(List<Product> products){
        System.out.println("===================================欢迎使用指定商品下架功能【修改前】===================================");
        System.out.println("商品编号\t\t商品名称\t\t商品状态");

        for (Product p : products) {
            System.out.println(p.getId()+"\t\t"+p.getPname()+"\t\t"+p.getState());
        }

        System.out.println("======================================================================");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要下架的商品编号：");
        String id = sc.nextLine();

        for (Product p : products) {
            if (p.getState().equals("上架")&&p.getId().equals(id)){
                p.setState("下架");
                System.out.println("商品下架成功...");
                show(products);
            }else if (p.getId().equals(id)){
                System.out.println("抱歉，该商品已经下架...");
            }
        }
        XmlUtils.write("综合应用/T8/product.xml",products,false);

    }

    public static void show(List<Product> products){
        System.out.println("===================================欢迎使用指定商品下架功能【修改后】===================================");
        System.out.println("商品编号\t\t商品名称\t\t商品状态");
        for (Product p : products) {
            System.out.println(p.getCid()+"\t\t"+p.getPname()+"\t\t"+p.getState());
        }


    }

    // 1.4---(后台管理平台)---【2】修改用户状态
    public static void Mus(List<User> userList) {
        System.out.println("===================================欢迎使用修改用户状态功能===================================");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要禁用的用户姓名：");
        String username = sc.nextLine();
        System.out.println("请输入要禁用的用户手机号：");
        String phone = sc.nextLine();
        for (User user : userList) {
            if (user.getUsername().equals(username) && user.getPhone().equals(phone)&& user.getRole().equals("管理员")) {
                System.out.println("抱歉，管理员用户暂不允许修改...");
            } else if (user.getUsername().equals(username)&& user.getPhone().equals(phone) && user.getRole().equals("普通用户")) {
                if (user.getState().equals("禁用")) {
                    System.out.println("该用户已经被禁用！");
                } else if (user.getState().equals("启用")) {
                    user.setState("禁用");
                    System.out.println("【"+username+"】禁用成功！");
                    Vui(userList);
                }
            }
        }
        XmlUtils.write("综合应用/T8/user.xml",userList,false);
    }

    // 1.4---(后台管理平台)---【1】查看用户信息
    public static void Vui(List<User> userList) {
        System.out.println("===================================欢迎使用查看用户信息功能===================================");
        System.out.println("用户名\t\t手机号\t\t是否禁用\t\t权限");
        for (User user : userList) {
            System.out.println(user.getUsername() + "\t\t" + user.getPhone() + "\t\t" + user.getState() + "\t\t" + user.getRole());
        }
    }

    // 1.4---(后台管理平台)
    public static void Bmp(List<User> userList,List<Product> product, List<Category> category) {
        while (true) {
            System.out.println("===================================后台管理平台===================================");
            System.out.println("【1】查看用户信息");
            System.out.println("【2】修改用户状态");
            System.out.println("【3】指定商品下架");
            System.out.println("【4】删除指定商品类别");
            System.out.println("【5】退出");

            Scanner sc = new Scanner(System.in);
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    Vui(userList);
                    break;
                case 2:
                    Mus(userList);
                    break;
                case 3:
                    Dmrfs(product);
                    break;
                case 4:
                    Dtspc(category);
                    break;
                case 5:
                    return;
                default:
                    System.out.println("请输入正确数字");
            }
        }
    }
    // 1.4---(网上商品前台管理平台)---【1】查看商品详情信息
    public static void Vpd(List<Category> category){
        System.out.println("商品名称\t\t商品颜色\t\t商品内存\t\t单价\t\t所属类别\t\t商品状态");
        for (Category c : category) {
            List<Product> productList = c.getProductList();
            for (Product p : productList) {
                List<ProductDetails> productDetails = p.getpList();
                for (ProductDetails pp : productDetails) {
                    if (p.getState().equals("上架")){
                        System.out.println(p.getPname()+"\t\t"+pp.getColor()+"\t\t"+pp.getMemory()+"\t\t" +pp.getPrice()+"\t\t"+c.getCname()+"\t\t"+p.getState());
                    }
                }
            }
        }

    }

    // 1.4---(网上商品前台管理平台)
    public static void Ocfdmp(List<Category> category) {
        System.out.println("===================================网上商城前台管理平台===================================");
        System.out.println("【1】查看商品详情信息");
        System.out.println("【2】退出");

        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        switch (num){
            case 1:
                Vpd(category);
                break;
            case 2:
                return;
            default:
                System.out.println("请输入正确数字");
        }
    }

    // 1.4
    public static void Register(List<User> userList,List<Category> category,List<Product> product) throws IOException {
        System.out.println("===================================欢迎使用登录功能===================================");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String pwd = sc.nextLine();
        StringBuilder sb = new StringBuilder();
        String date = new SimpleDateFormat("yyyy年MM月dd日 HH时:mm分:ss秒:").format(new Date());
        File file = new File("综合应用/T8/login.txt");

        for (User user : userList) {
            if (user.getUsername().equals(username) && user.getPassword().equals(pwd)) {
                if (user.getState().equals("启用")) {
                    if (user.getRole().equals("普通用户")) {
                        sb.append(date).append("【").append(username).append("】").append(user.getRole()).append("登录，登录成功！");
                        System.out.println(user.getRole() + "登录，登录成功！");
                        FileUtils.writeStringToFile(file, sb.toString() + "\n", true);
                        Ocfdmp(category);
                        return;
                    } else if (user.getRole().equals("管理员")) {
                        sb.append(date).append("【").append(username).append("】").append(user.getRole()).append("登录，登录成功！");
                        System.out.println(user.getRole() + "登录，登录成功！");
                        FileUtils.writeStringToFile(file, sb.toString() + "\n", true);
                        Bmp(userList,product,category);
                        return;
                    }
                } else {
                    sb.append(date).append("【").append(username).append("】").append("该用户已经被禁用，请联系管理员...");
                    System.out.println("该用户已经被禁用，请联系管理员...");
                    FileUtils.writeStringToFile(file, sb.toString() + "\n", true);
                    return;
                }
            }
        }
        System.out.println("用户名或密码错误，登录失败，请重新登录！");
        sb.append("用户名或密码错误，登录失败，请重新登录！");
        FileUtils.writeStringToFile(new File("综合应用/T8/login.txt"), sb.toString() + "\n", true);

    }
    // 把 prodict 存到 category的集合里
    public static List<Category> getCategory( List<Product> products){
        List<Category> categories = XmlUtils.readAll("综合应用/T8/category.xml", Category.class);
        for (Category c : categories) {
            ArrayList<Product> list = new ArrayList<>();
            for (Product p : products) {
                if (c.getId().equals(p.getCid())){
                    p.setCategory(c);
                    list.add(p);
                }
            }
            c.setProductList(list);
        }
        return categories;
    }
    // 把 prodictDetails 存到 prodict 集合里
    public static List<Product> getProduct(){
        List<Product> products = XmlUtils.readAll("综合应用/T8/product.xml", Product.class);
        List<ProductDetails> productsD = XmlUtils.readAll("综合应用/T8/productDetails.xml", ProductDetails.class);
        for (Product p1 : products) {
            ArrayList<ProductDetails>list = new ArrayList<>();
            for (ProductDetails p2 : productsD) {
                if (p1.getId().equals(p2.getProductId())){
                    p2.setProduct(p1);
                    list.add(p2);

                }
            }
            p1.setpList(list);
        }

        return products;
    }
}
